package com.li.link;

public class DoubleLinst {
    private HereNode2 head = new HereNode2(0, "", "");

    public HereNode2 getHead() {
        return head;
    }

    //显示链表
    public void list() {
        if (head.next == null) {
            System.out.println("链表空");
            return;
        }
        HereNode2 temp = head.next;
        while (true) {
            if (temp == null) {
                break;
            }
            System.out.println(temp);
            temp = temp.next;
        }
    }

    public void add(HereNode2 hereNode) {
        HereNode2 temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            temp = temp.next;
        }
        temp.next = hereNode;
        hereNode.pre = temp;
    }

    public void update(HereNode2 hereNode) {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }

        HereNode2 temp = head.next;
        boolean flag = false;
        while (true) {
            if (temp == null) {
                break;
            }
            if (temp.no == hereNode.no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }

        if (flag) {
            temp.name = hereNode.name;
            temp.nickname = hereNode.nickname;

        } else {
            System.out.printf("没有找到%d", hereNode.no);
        }
    }

    public void delete(int no) {
        if (head.next == null) {
            System.out.println(":链表空");
            return;
        }
        HereNode2 temp = head.next;
        boolean flag = false;
        while (true) {
            if (temp == null) {
                break;
            }
            if (temp.no == no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {

            temp.pre.next = temp.next;

            if (temp.next != null) {
                temp.next.pre = temp.pre;
            }

        } else {
            System.out.printf("删除的%d节点不存在\n", no);
        }
    }

    public void addbyorder(HereNode2 hereNode) {
        //复制节点temp，位于添加位置前一个结点
        HereNode2 temp = head;
        boolean flag = false;
        while (true) {
            if (temp.next == null) {//temp到链表最后
                break;
            }
            if (temp.next.no > hereNode.no) {//位置找到
                break;
            } else if (temp.next.no == hereNode.no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            System.out.printf("准备插入编号%d 已经存在，不能加入\n", hereNode.no);
        } else {
            if (temp.next == null) {//temp到链表最后
                add(hereNode);
            } else {
                hereNode.pre = temp;
                hereNode.next = temp.next;
                temp.next.pre = hereNode;
                temp.next = hereNode;
            }


        }

    }
}

//双向列表
class HereNode2 {
    public int no;
    public String name;
    public String nickname;
    public HereNode2 next;
    public HereNode2 pre;

    public HereNode2(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;

    }

    @Override
    public String toString() {
        return "HereNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +

                '}';
    }
}
